﻿using System;

namespace DotNetCommon.Validate.Annotations;

/// <summary>
/// 必须大于0, 示例
/// <code>
/// public class Person
/// {
///     [VMustGreaterThanZero]
///     public int Id { get; set; }
///     [VMustGreaterThanZeroIfNotNull]
///     public double? Score { get; set; }
/// }
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustGreaterThanZeroAttribute(string errorMessage = null) : Attribute, IValidateAction
{
    public string ErrorMessage { get; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        ValidateExtensions.MustGreaterThanInternal(ctx, 0, ErrorMessage);
    }
}
/// <summary>
/// 如果不为null, 则必须大于0, 示例
/// <code>
/// public class Person
/// {
///     [VMustGreaterThanZero]
///     public int Id { get; set; }
///     [VMustGreaterThanZeroIfNotNull]
///     public double? Score { get; set; }
/// }
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustGreaterThanZeroIfNotNullAttribute(string errorMessage = null) : Attribute, IValidateAction
{
    public string ErrorMessage { get; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        if (ctx.ModelValue == null) return;
        ValidateExtensions.MustGreaterThanInternal(ctx, 0, ErrorMessage);
    }
}